package com.spa.domain.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spa.application.command.member.AddressesAddCommand;
import com.spa.domain.entity.UserAddress;
import com.spa.infrastructure.enums.dict.base.WhetherTypeEnum;
import com.spa.infrastructure.mapper.UserAddressMapperStruct;
import com.spa.infrastructure.repository.condition.UserAddressCondition;
import com.spa.infrastructure.repository.po.UserAddressPO;
import com.spa.infrastructure.repository.service.UserAddressRepositoryService;
import com.spa.infrastructure.util.SessionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址  domain
 *
 * @author: lig
 * @date: 2023/12/7
 */
@Service
public class AddressService {

    @Resource
    private UserAddressRepositoryService userAddressRepositoryService;

    /**
     * 添加用户地址信息
     *
     * @author: lig
     * @date: 2023/12/6
     */
    @Transactional
    public UserAddress addUserAddress(AddressesAddCommand command, Long extUserId) {

        UserAddressPO userAddressPO = UserAddressMapperStruct.INSTANCE.command2MemberPo(command);

        if (isOne(SessionUtil.getUser().getExtUserId())) {
            userAddressPO.setIsDefault(WhetherTypeEnum.$0.getCode());
        } else {
            userAddressPO.setIsDefault(WhetherTypeEnum.$1.getCode());
        }

        userAddressPO.setUserId(extUserId);
        userAddressRepositoryService.save(userAddressPO);

        return UserAddressMapperStruct.INSTANCE.po2Entity(userAddressPO);
    }

    /**
     * 修改
     *
     * @author: lig
     * @date: 2023-12-21
     */
    public void editAddress(UserAddressPO po) {
        userAddressRepositoryService.updateById(po);
    }

    /**
     * 删除地址
     *
     * @param id 地址ID
     * @author: lig
     * @date: 2023-12-21
     */
    @Transactional
    public void delAddressById(long id) {
        userAddressRepositoryService.removeById(id);
    }

    /**
     * 地址详情  通过ID
     *
     * @author: lig
     * @date: 2023-12-09
     */
    public UserAddress detailsById(Long id) {
        return UserAddressMapperStruct.INSTANCE.po2Entity(userAddressRepositoryService.getById(id));
    }

    public Page<UserAddress> page(UserAddressCondition condition) {
        return UserAddressMapperStruct.INSTANCE.po2Entity4Page(userAddressRepositoryService.queryPage(condition));

    }

    /**
     * 修改默认值
     *
     * @author: lig
     * @date: 2023/12/6
     */
    @Transactional
    public boolean editDefault(Long userId, Long addressId) {

        //该用户的所有默认值为空
        userAddressRepositoryService.clearDefaultByUserId(userId);

        //设置默认值
        return userAddressRepositoryService.isDefault(addressId);
    }

    /**
     * 获取用户的默认地址
     *
     * @author: lig
     * @date: 2023/12/6
     */
    public UserAddress defaultInfo(Long userId) {
        return UserAddressMapperStruct.INSTANCE.po2Entity(userAddressRepositoryService.defaultInfoByUserId(userId));
    }

    /**
     * 是否是第一条地址
     *
     * @author: lig
     * @date: 2023/12/6
     */
    public boolean isOne(Long userId) {
        //获取关联表
        List<UserAddressPO> userAddressList = userAddressRepositoryService.listByUserId(userId);
        if (CollUtil.isNotEmpty(userAddressList)) {
            return false;
        }
        return true;

    }

    /**
     * 通过用户ID 获取地址列表
     *
     * @author: lig
     * @date: 2023/12/22
     */
    public List<UserAddress> listByUserId(Long userId) {

        UserAddressCondition condition = new UserAddressCondition();
        condition.setUserId(userId);
        List<UserAddressPO> addressPOS = userAddressRepositoryService.list(condition);
        return UserAddressMapperStruct.INSTANCE.po2Entity4List(addressPOS);
    }

    /**
     * 是否是用户地址
     *
     * @author: lig
     * @date: 2023/12/22
     */
    public boolean isUserAddress(Long userId, Long addressId) {
        return userAddressRepositoryService.isUserAddress(userId,addressId);
    }

}
